﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace SimpleUnitity.Cache
{
    [Serializable]
    public class NormalCache : ICache
    {
        private IDictionary<string, object> datas;

        public NormalCache(bool ignoreCase = false)
        {
            if (ignoreCase)
            {
                datas = new Dictionary<string, object>(StringComparer.CurrentCultureIgnoreCase);
            }
            else
            {
                datas = new Dictionary<string, object>(StringComparer.CurrentCulture);
            }
        }

        public NormalCache(IDictionary<string, object> dicts)
        {
            datas = new Dictionary<string, object>(dicts);
        }

        public void Clear()
        {
            datas.Clear();
        }

        public bool Exists(string key)
        {
            return datas.ContainsKey(key);
        }

        public void Set(string key, object value)
        {
            if (datas.ContainsKey(key))
            {
                datas[key] = value;
            }
            else
            {
                datas.Add(key, value);
            }
        }

        public object Get(string key, object defaultValue = null)
        {
            if (datas.ContainsKey(key))
            {
                return datas[key];
            }
            else
            {
                return defaultValue;
            }
        }

        public void Remove(string key)
        {
            if (datas.ContainsKey(key))
            {
                datas.Remove(key);
            }
        }

        public ICollection<string> Keys
        {
            get { return datas.Keys; }
        }
    }
}
